package com.pan.controller;


import com.pan.entity.Goods;
import com.pan.entity.Result;
import com.pan.service.GoodsService;
import com.pan.vo.GoodsCreateVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;


    @ResponseBody
    @PostMapping("/create")
    public Result createGoods(@CookieValue("adminTicket") String adminTicket,
                              @RequestBody GoodsCreateVo goodsCreateVo){
        Result result = goodsService.insert(adminTicket,goodsCreateVo);
        return result;
    }

    @ResponseBody
    @PutMapping("/update/{updateMemberName}")
    public Result updateGoods(@CookieValue("adminTicket") String adminTicket,
                              @PathVariable("updateMemberName")String updateMemberName,
                              @RequestParam("updateValue") Object updateValue,
                              @RequestParam("id") Long id){
        Result result  =  goodsService.update(adminTicket,id,updateValue,updateMemberName);
        return result;
    }

    @GetMapping("/select/{goodsId}")
    @ResponseBody
    public Result<Goods> selectById(@PathVariable("goodsId") Long goodsId){
        Result<Goods> result = goodsService.selectById(goodsId);
        return result;
    }

    @GetMapping("/select/category/{categoryId}")
    @ResponseBody
    public Result<List<Goods>> selectByCategoryId(@PathVariable("categoryId") Long categoryId){
        Result<List<Goods>> result = goodsService.selectByCategory(categoryId);
        return result;
    }

    @GetMapping("/select/random/{count}")
    @ResponseBody
    public Result<List<Goods>> selectByCategoryId(@PathVariable("count") Integer count){
        Result<List<Goods>> result = goodsService.selectByRandom(count);
        return result;
    }

    @GetMapping("/select/list")
    @ResponseBody
    public Result<List<Goods>> selectGoodsList(@RequestParam("idList") List<Long> idList){
        Result<List<Goods>> result = goodsService.selectGoodsList(idList);
        return result;
    }


}
